package icraf.carbon.gui;

import icraf.carbon.core.CostBenefitUnit;
import icraf.carbon.core.DataItem;
import icraf.carbon.gui.MenuFactory.MenuProperty;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.ListModel;
import javax.swing.SwingUtilities;

public class DataItemListPanel extends JPanel  {
	
	class DataItemListModel extends MatrixTableModel {
		private static final long serialVersionUID = 8968868974903218229L;
		private String[] headers = {MenuProperty.PROJECT_ZONE.getLabel(), 
				MenuProperty.PROJECT_LANDUSE.getLabel() +" 1",
				MenuProperty.PROJECT_LANDUSE.getLabel() +" 2",
				MenuProperty.PROJECT_NPV.getLabel(), 
				MenuProperty.PROJECT_EMISSION.getLabel()};
		private NumberRowHeader tableListModel= new NumberRowHeader(this);
		
		
		@Override
		public Class<?> getColumnClass(int column) {
			if(column >= 3)
				return Double.class;
			return String.class;
		}

		@Override
		public int getColumnCount() {
			return headers.length;
		}

		public String getColumnName(int col) {
			return headers[col];
		}

		@Override
		public int getRowCount() {
			if(data == null) return 0;
			return data.size();
		}

		@Override
		public Object getValueAt(int row, int column) {
			if(data == null) return null;
			DataItem d = data.get(row);
			switch(column) {
				case 0: return d.getZone();
				case 1: return d.getLandCover1(); 
				case 2: return d.getLandCover2();
				case 3: return d.getCost(costBenefitUnit);
				case 4: return d.getEmission();
			}
			return null;
		}

		@Override
		public ListModel<Object> getRowHeaderModel() {
			return tableListModel;
		}
		
		
		@Override
		public void fireTableDataChanged() {
			super.fireTableDataChanged();
			tableListModel.fireContentChange(this.getRowCount());
		}



	}

	
	
	private static final long serialVersionUID = -1812214166165551697L;

	public static void main(String[] args) {
		final JFrame frame = new JFrame();
		frame.setSize(new Dimension(300, 300));
		DataItemListPanel p = new DataItemListPanel();
		frame.getContentPane().add(p, BorderLayout.CENTER);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}

		});
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				frame.setVisible(true);
			}
		});

	}

	private MatrixPanel matrixPanel;
	private DataItemListModel dataModel = new DataItemListModel();
	private ArrayList<DataItem> data;
	private CostBenefitUnit costBenefitUnit;
	
	public DataItemListPanel() {
		setLayout(new BorderLayout());
		JPanel mainPanel = new JPanel(new BorderLayout());
		matrixPanel = new MatrixPanel(dataModel);
		matrixPanel.setPreferredSize(new Dimension(200,200));
		matrixPanel.getTable().setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
		mainPanel.add(matrixPanel);
		this.add(mainPanel);


	}

	public ArrayList<DataItem> getData() {
		return data;
	}

	public void setData(ArrayList<DataItem> data, CostBenefitUnit costBenefitUnit) {
		this.data = data;
		this.costBenefitUnit = costBenefitUnit;
		dataModel.fireTableDataChanged();
	}

}
